$(function(){
	init();
 
	$('#tree3').fileTree(
			{
				root: '00', 
				script: 'role.do?type=methodTree'//统一调用角色的查看所有功能树
			},function(file,name,id){

			},function(file,name,id){

			});
	
	//方法修改对话框加载
	$("#methodTree").dialog({
		draggable:false,
		bgiframe: true,
		autoOpen:false,
		width:screen.width - 300,//js自适应屏幕分辨率宽度，对应的还有sreen.height。300为减去左侧的树状菜单固定宽度（粗略计算）
		height:400,
		open:function(event,ui)
		{
			$(event.target).width ($(event.target).dialog("option","width "));//可能有不加空格纵向下拉条变窄
			$(event.target).height ($(event.target).dialog("option","height"));
		},
		stack:false,
		modal:true,
		resizable: false,
		position:'center',
		title:'功能方法架构',
		buttons:{"取消":function(){$("#methodTree").dialog("close");},"保存":addMethod}
	});
	
	//管理架构树形菜单数据加载(数据范围)
	$("#tree5").tree({			
		data : { 
			async : true,
			opts : {					
				url : "operator.do?type=frame"
			}
		},
		callback:{
			beforedata:function(NODE, TREE_OBJ) {
				 return { dir : $(NODE).attr("id") || "" } ;				 
			}
			,				
			onselect:function(NODE){
				selectFrame2($(NODE).attr("id"),$(NODE).children("a").text(),$(NODE).attr("title"));
			}
		},
		 lang: {
        	loading: "数据加载中……"
   		 },
        ui: {
            theme_name	: "apple"
        }
	});	
	
	
	
});

function init()
{
	 $.ajaxSetup({cache:false});//ajax调用不使用缓存  
	 initGrid();
	 initSysManaTree();
	 initSysRoleList();
	
	 
	 $("#maxDate").datepicker({
	 	dateFormat:'yy-mm-dd',
	 	
	 	beforeShow: function (i, e) { 
                var z = jQuery(i).closest(".ui-dialog").css("z-index") + 4; 
                e.dpDiv.css('z-index', z); 
        } 
	 	
	 });
	 
	  $("#smfName").click(function(){
	  	$("#sysManaTree").dialog("open");
	  });
	  
	  $("#operatorRole").click(function(){
	  	var roleIDList = $("#operatorRoleID").val();
	  	$("#roleList li input:checkbox").each(function(){
	  		$(this).removeAttr("checked");
	  	});
	  	$.each(roleIDList.split(";"),function(i,n){
	  		$("#roleList li input:checkbox[value="+n+"]").attr("checked","checked");
	  	});
	  	$("#sysRoleList").dialog("open");
	  });
		
		//数据范围管理窗口加载
		$("#dataArea").dialog({
			bgiframe: true,
			autoOpen:false,
			width:440,
			stack:false,
			modal:true,
			position:'center',
			title:'数据范围管理',
			buttons:{"取消":function(){
								$("#dataArea").dialog("close");
							}
			}
		});
		
		//地区选择（管理架构）树形菜单显示窗口加载
		$("#areaTree").dialog({
			bgiframe: true,
			autoOpen:false,			
			width:440,
			height:360,
			open:function(event,ui)
			{
				$(event.target).height ($(event.target).dialog("option","height"));
			},
			resizable: false,
			stack:false,
			modal:true,
			draggable: false,
			position: 'center',
			title:'营销架构',
			buttons:{"取消":function(){
								$("#areaTree").dialog("close");
							},
					"保存":function(){
								var str = "";
								$("#tree5 input[title='Y']:checked").each(function(){
									str = str + $(this).val() + ","
								});

								if($.trim(str).length > 0){
									str = str.substring(0,str.length - 1);
								}
								var selectedRowID = $("#operatorList").getGridParam("selrow");
								$.post(
									"operator.do?type=addDataScope",
									{
										opId:selectedRowID,
										str:str
									},function(xml){
										var flag = $(xml).find("flag").text();
										var body = $(xml).find("flag").text();
										if(flag == 1){
											alert("修改成功");
											$("#areaTree").dialog("close");
											$("#dataArea").dialog("close");
										}
									},"xml"
								);					
							}
			}
		});
}

function initSysManaTree()
{
	
	
	$("#tree").tree(
	{
			
			data : { 
				async : true,
				opts : {
					url : "frameex.do?type=frameexTreeLoading"
				}
			},
			callback:{
				beforedata:function(NODE, TREE_OBJ) 
				{
					 return { dir : $(NODE).attr("id") || "" } ;
				},
				onselect:function(NODE)
				{
					selectFrame($(NODE).attr("id"),$(NODE).children("a").html());
				}
			},
			 lang: {
            	loading: "数据加载中……"
       		 },
	        ui: {
	            theme_name	: "apple"
	        }
		});
		
		$("#sysManaTree").dialog({
			bgiframe: true,
			autoOpen:false,
			width:440,
			stack:false,
			modal:true,
			position:'center',
			title:'系统组织架构'	
		});
		
		
		
}

function initSysRoleList()
{
	$.post(
	"role.do?type=listAll",
	{},
	function(xml){
		$(xml).find("Role").each(function(){
			var roleID = $(this).find("ID").text();
			var roleName = $(this).find("name").text();
			var liElement = $("<li></li>");
			$("<input type='checkbox'/>").val(roleID).appendTo(liElement);
			$("<label></label>").html(roleName +"["+roleID+"]").appendTo(liElement);
			liElement.appendTo($("#roleList"));
		});
	},
	"xml"
	);
	
	$("#sysRoleList").dialog({
			bgiframe: true,
			autoOpen:false,
			width:320,
			height:420,
			stack:false,
			modal:true,
			position:'center',
			title:'系统角色',
			buttons:{"取消":function(){$("#sysRoleList").dialog("close");},"完成":selectRole}	
		});
}

function selectFrame(id,name)
{
	$("#detailDialog #smfID").val(id);
	$("#detailDialog #smfName").val(name);
	$("#sysManaTree").dialog("close");
}



function removeLastSign(str,sign)
{
	if(!sign)
	{
	 	sign=";" ;
	}
	if(str)
	{
		if(str.endWith(sign))
		{
			str = str.substr(0,str.length-1);
		}
	}
	
	return str; 
}


function selectRole()
{
	var roleNameList="";
	var roleIDList="";
	
	$("#roleList input:checked").each(function(){
		roleNameList +=$(this).next("label").html().substr(0,$(this).next("label").html().length-12)+";";
		roleIDList +=$(this).val()+";";  
	});
	
	roleNameList = removeLastSign(roleNameList);
	roleIDList = removeLastSign(roleIDList);
	
	$("#operatorRole").val(roleNameList);
	$("#operatorRoleID").val(roleIDList);
	
	$("#sysRoleList").dialog("close");
}

function updateRole()
{
	//check condition
	if($.trim($("#roleDialog #idR").val()).length ===0)
	{
		alert("系统编码不能为空");
		$("#roleDialog #idR").focus();
		return;
	}
	
	if($.trim($("#roleDialog #operatorRoleID").val()).length ===0)
	{
		if(!window.confirm("是否清除用户所有角色")) return;
		
		if($("#roleDialog #operatorRoleID").val().split(";").length != $("#roleDialog #operatorRole").val().split(";").length)
		{
			alert("角色选择出错，请重新选择");
			$("#roleDialog #operatorRole").focus();
			return;
		}
		
	}
	
	$.post(
			"role.do?type=updateOperaotrRole",
			{
				operatorID:$("#roleDialog #idR").val(),
				roleIDs:$("#roleDialog #operatorRoleID").val()
			},
			function(xml){
				 var flag   = parseInt($(xml).find("flag").text());
				 var body = $(xml).find("body").text();
				 if(flag === 1)
				 {
				 	alert("更新用户角色成功");
				 	$("#roleDialog").dialog("close");
				 }
				 else
				 {
				 	alert(body);
				 }
			},
			"xml");
}

function processDelete()
{
	 var selectedRowID = $("#operatorList").getGridParam("selrow");
	 if(!selectedRowID)
	 {
	 		var alertIDs = {themodal:'alertmod',modalhead:'alerthd',modalcontent:'alertcnt'};
	 		viewModal("#"+alertIDs.themodal,{gbox:"#gbox_"+"operatorList",jqm:true}); 
	 		$("#jqg_alrt").focus();
	 		return;
	 }
	 
	 
	$("#detailDialog").parent().find(".ui-dialog-buttonpane button:contains('增加')").hide();
	$("#detailDialog").parent().find(".ui-dialog-buttonpane button:contains('删除')").show();
	$("#detailDialog").parent().find(".ui-dialog-buttonpane button:contains('保存')").hide();
	
	initDialogForm();
	loadingSelectedRowData();
	$("#detailDialog input").add("select").add("textarea").each(function(){
		$(this).attr("disabled",true);
	});
	
	
	$("#detailDialog").dialog("open");
	
}

function processEdit()
{
	$("#detailDialog").parent().find(".ui-dialog-buttonpane button:contains('增加')").hide();
	$("#detailDialog").parent().find(".ui-dialog-buttonpane button:contains('删除')").hide();
	$("#detailDialog").parent().find(".ui-dialog-buttonpane button:contains('保存')").show();
	initDialogForm();
	loadingSelectedRowData();
	$("#detailDialog").dialog("open");
}

function processAdd()
{
	$("#detailDialog").parent().find(".ui-dialog-buttonpane button:contains('增加')").show();
	$("#detailDialog").parent().find(".ui-dialog-buttonpane button:contains('删除')").hide();
	$("#detailDialog").parent().find(".ui-dialog-buttonpane button:contains('保存')").hide();
	
	initDialogForm();
	$("#detailDialog input:not(.readonly):first").focus();
	$("#detailDialog").dialog("open");
	
}

function processDetail()
{
	var selectedRowID = $("#operatorList").getGridParam("selrow");
	 if(!selectedRowID)
	 {
	 		var alertIDs = {themodal:'alertmod',modalhead:'alerthd',modalcontent:'alertcnt'};
	 		viewModal("#"+alertIDs.themodal,{gbox:"#gbox_"+"operatorList",jqm:true}); 
	 		$("#jqg_alrt").focus();
	 		return;
	 }
	 
	 
	$("#detailDialog").parent().find(".ui-dialog-buttonpane button:contains('增加')").hide();
	$("#detailDialog").parent().find(".ui-dialog-buttonpane button:contains('删除')").hide();
	$("#detailDialog").parent().find(".ui-dialog-buttonpane button:contains('保存')").hide();
	
	initDialogForm();
	loadingSelectedRowData();
	$("#detailDialog input").add("select").add("textarea").each(function(){
		$(this).attr("disabled",true);
	});
	
	
	$("#detailDialog").dialog("open");
}


function processRole()
{
	var selectedRowID = $("#operatorList").getGridParam("selrow");
	 if(!selectedRowID)
	 {
	 		var alertIDs = {themodal:'alertmod',modalhead:'alerthd',modalcontent:'alertcnt'};
	 		viewModal("#"+alertIDs.themodal,{gbox:"#gbox_"+"operatorList",jqm:true}); 
	 		$("#jqg_alrt").focus();
	 		return;
	 }
	 
	 $("#roleDialog #idR").val(selectedRowID);
	 $.post(
	 "role.do?type=queryByOperator",
	 {
	 	operatorID:selectedRowID
	 },
	 function(xml){
	 	var roleNameList="";
		var roleIDList="";
		
		$(xml).find("Role").each(function(){
	 		roleNameList += $(this).find("name").text()+";";
			roleIDList +=$(this).find("ID").text()+";";  
	 	});
	
		roleNameList = removeLastSign(roleNameList);
		roleIDList = removeLastSign(roleIDList);
		
		$("#operatorRole").val(roleNameList);
		$("#operatorRoleID").val(roleIDList);
	 	
	 	$("#roleDialog").dialog("open");
	 },"xml");
	 
	 
}

function processUpdatePassword()
{
	var selectedRowID = $("#operatorList").getGridParam("selrow");
	 if(!selectedRowID)
	 {
	 		var alertIDs = {themodal:'alertmod',modalhead:'alerthd',modalcontent:'alertcnt'};
	 		viewModal("#"+alertIDs.themodal,{gbox:"#gbox_"+"operatorList",jqm:true}); 
	 		$("#jqg_alrt").focus();
	 		return;
	 }
	 
	 //init
	 $("#updatePasswordDialog #idU").val(selectedRowID);
	 $("#updatePasswordDialog #passwordU").val('123456');
	 
	$("#updatePasswordDialog input:not(.readonly):first").focus();
	$("#updatePasswordDialog").dialog("open");
}

function updatePassword()
{
	//valid
	if($.trim($("#updatePasswordDialog #passwordU").val()).length === 0)
	{
		alert("新密码不能为空");
		$("#updatePasswordDialog #passwordU").focus();
		return;
	}
	
	$.post(
		"operator.do?type=updatePassword",
		{
			id:$("#updatePasswordDialog #idU").val(),
	 		password:$("#updatePasswordDialog #passwordU").val()
		},
		function(xml)
		{
			 var flag   = parseInt($(xml).find("flag").text());
			 var body = $(xml).find("body").text();
			 if(flag === 1)
			 {
			 	alert("更新密码成功,新密码为:"+$("#updatePasswordDialog #passwordU").val());
			 	$("#updatePasswordDialog").dialog("close");
			 }
			 else
			 {
			 	alert(body);
			 }
						 
		},
		"xml"
	);
	
	
}

function initDialogForm()
{
	
	$("#detailDialog input").add("select").add("textarea").each(function(){
		$(this).removeAttr("disabled");
	});
	$("#detailDialog input").add("textarea").each(function(){
		if($(this).attr("id") == "maxDate")
		{
			$(this).val("2015-12-31");
		}
		else
		{
			$(this).val("");
		}
	});
	
	$("detailDialog select").each(function(){
		$(this).val($(this).find("option:first").val());
	});
}

function loadingSelectedRowData()
{
	 var selectedRowID = $("#operatorList").getGridParam("selrow");
	 
	 $.post(
	 	"operator.do?type=detail",
	 	{
	 		operatorID:selectedRowID
	 	},
	 	function(xml)
	 	{
	 		$("#detailDialog #id").val( $(xml).find("Operator opID").text());
	 		$("#detailDialog #workNO").val( $(xml).find("Operator opWorkno").text());
	 		$("#detailDialog #name").val( $(xml).find("Operator opName").text());
	 		$("#detailDialog #job").val( $(xml).find("Operator opJob").text());
	 		//$("#detailDialog #password").val( $(xml).find("Operator opPassword").text());
	 		//$("#detailDialog #password").val( $(xml).find("Operator opPassword").text());
	 		$("#detailDialog #smfName").val( $(xml).find("Operator opSmfName").text());
	 		$("#detailDialog #smfID").val( $(xml).find("Operator opSmfID").text());
	 		$("#detailDialog #status").val( $(xml).find("Operator opStatus").text());
	 		$("#detailDialog #maxDate").val( $(xml).find("Operator opMaxDate").text());
	 		$("#detailDialog #tel").val( $(xml).find("Operator opTel").text());
	 		$("#detailDialog #qq").val( $(xml).find("Operator opQQ").text());
	 		$("#detailDialog #email").val( $(xml).find("Operator opEmail").text());
	 		$("#detailDialog #memo").val( $(xml).find("Operator opMemo").text());
	 		
	 		
	 	},
	 	"xml"
	 )
	
}


function validOpertor()
{
	var validFlag = true;
	
	$("#detailDialog input").add("select").each(function(){
		
		
		if(validFlag && $(this).next("span.required").size()>0  && $.trim($(this).val()).length == 0)
		{
			alert($(this).parent().children("label").html() + "不能为空");
			$(this).focus();
			validFlag = false;
			return;
		}
	});
	
	return validFlag;
	
}

function addOperator()
{
	if(!validOpertor())
	{
		return;
	}
	
	$.post(
		"operator.do?type=insert",
		{
	 		workNO:$("#detailDialog #workNO").val(),
	 		name:$("#detailDialog #name").val(),
	 		job:$("#detailDialog #job").val(),
	 		smfID:$("#detailDialog #smfID").val(),
	 		status:$("#detailDialog #status").val(),
	 		maxDate:$("#detailDialog #maxDate").val(),
	 		tel:$("#detailDialog #tel").val(),
	 		qq:$("#detailDialog #qq").val(),
	 		email:$("#detailDialog #email").val(),
	 		memo:$("#detailDialog #memo").val()
		},
		function(xml)
		{
			 var flag   = parseInt($(xml).find("flag").text());
			 var body = $(xml).find("body").text();
			 $("#detailDialog #id").val(body)  ;
			 
			 if(flag === 1)
			 {
			 	var dataRow = 
			 	{
			 		opID:$("#detailDialog #id").val(),
			 		opWorkno:$("#detailDialog #workNO").val(),
			 		opName:$("#detailDialog #name").val(),
			 		opJob:$("#detailDialog #job").val(),
			 		opSmfID:$("#detailDialog #smfID").val(),
			 		opSmfName:$("#detailDialog #smfName").val(),
			 		opStatus:$("#detailDialog #status").val(),
			 		opStatusName:$("#detailDialog #status option:selected").text(),
			 		maxDate:$("#detailDialog #maxDate").val(),
			 		opTel:$("#detailDialog #tel").val(),
			 		opQQ:$("#detailDialog #qq").val()
			 	}
			 	
			 	$("#operatorList").addRowData( $("#detailDialog #id").val(),dataRow);
			 	
			 	alert("新增用户成功，用户系统编码为:"+body);
			 	$("#detailDialog").dialog("close");
			 }
			 else
			 {
			 	alert(body);
			 }
						 
		},
		"xml"
	);
	
}

function updateOperator()
{
	if(!validOpertor())
	{
		return;
	}
	
	$.post(
		"operator.do?type=update",
		{
			id:$("#detailDialog #id").val(),
	 		workNO:$("#detailDialog #workNO").val(),
	 		name:$("#detailDialog #name").val(),
	 		job:$("#detailDialog #job").val(),
	 		smfID:$("#detailDialog #smfID").val(),
	 		status:$("#detailDialog #status").val(),
	 		maxDate:$("#detailDialog #maxDate").val(),
	 		tel:$("#detailDialog #tel").val(),
	 		qq:$("#detailDialog #qq").val(),
	 		email:$("#detailDialog #email").val(),
	 		memo:$("#detailDialog #memo").val()
		},
		function(xml)
		{
			 var flag   = parseInt($(xml).find("flag").text());
			 var body = $(xml).find("body").text();
			 if(flag === 1)
			 {
			 	
			 	
			 	var dataRow = 
			 	{
			 		opID:$("#detailDialog #id").val(),
			 		opWorkno:$("#detailDialog #workNO").val(),
			 		opName:$("#detailDialog #name").val(),
			 		opJob:$("#detailDialog #job").val(),
			 		opSmfID:$("#detailDialog #smfID").val(),
			 		opSmfName:$("#detailDialog #smfName").val(),
			 		opStatus:$("#detailDialog #status").val(),
			 		opStatusName:$("#detailDialog #status option:selected").text(),
			 		maxDate:$("#detailDialog #maxDate").val(),
			 		opTel:$("#detailDialog #tel").val(),
			 		opQQ:$("#detailDialog #qq").val()
			 	}
			 	
			 	$("#operatorList").setRowData( $("#operatorList").getGridParam("selrow"),dataRow);
			 	
			 	alert(body);
			 	$("#detailDialog").dialog("close");
			 }
			 else
			 {
			 	alert(body);
			 }
						 
		},
		"xml"
	);
}

function deleteOperator()
{
	if(window.confirm("确认删除"))
	{
		$.post(
		"operator.do?type=delete",
		{
			id:$("#detailDialog #id").val()
		},
		function(xml)
		{
			 var flag   = parseInt($(xml).find("flag").text());
			 var body = $(xml).find("body").text();
			 if(flag === 1)
			 {
			 	$("#operatorList").delRowData( $("#detailDialog #id").val());
			 	alert("删除成功");
			 	$("#detailDialog").dialog("close");
			 }
			 else
			 {
			 	alert(body);
			 }
						 
		},
		"xml"
		);
	}
}

function initGrid()
{
	
	$("#detailTabs").tabs({
		event: 'mouseover'
	});
	
	$("#detailDialog").dialog({
			bgiframe: true,
			autoOpen:false,
			width:620,
			stack:false,
			modal:true,
			position:'center',			
			title:'操作人员详细信息',
			buttons:{"取消":function(){$("#detailDialog").dialog("close");},"增加": addOperator,"保存":updateOperator,"删除":deleteOperator},
			open:function(){
				$("#detailDialog").parent().find(".ui-dialog-buttonpane button:visible").focus();
			}
			
	});
	
	$("#updatePasswordDialog").dialog({
			bgiframe: true,
			autoOpen:false,
			width:620,
			stack:false,
			modal:true,
			position:'center',			
			title:'操作人员密码修改',
			buttons:{"取消":function(){$("#updatePasswordDialog").dialog("close");},"保存":updatePassword},
			open:function(){
				$("#updatePasswordDialog").parent().find(".ui-dialog-buttonpane button:visible").focus();
			}
			
	});
	
	$("#roleDialog").dialog({
			bgiframe: true,
			autoOpen:false,
			width:620,
			stack:false,
			modal:true,
			position:'center',			
			title:'操作人员角色管理',
			buttons:{"取消":function(){$("#roleDialog").dialog("close");},"保存":updateRole},
			open:function(){
				$("#roleDialog").parent().find(".ui-dialog-buttonpane button:visible").focus();
			}
			
	});
	
	
	
	
	jQuery("#operatorList").jqGrid({
	   	url:'operator.do?type=list',
	   	height:450,
		datatype: "xml",
	   	colModel:[
	   				{name:'opID',label:'系统编码',index:'opID', width:100,xmlmap:'opID',align:'center'},
	   				{name:'opWorkno',label:'工号',index:'opWorkno', width:100,xmlmap:'opWorkno'},
	   				{name:'opName',label:'姓名',index:'opName', width:100,xmlmap:'opName'},
	   				{name:'opJob',label:'职位',index:'opjob', width:100,xmlmap:'opJob'},
	   				{name:'opSmfID',label:'管理架构',index:'opSmfID', width:100,hidden:true,xmlmap:'opSmfID',search:false},
	   				{name:'opSmfName',label:'管理架构',index:'opSmfName', width:100,xmlmap:'opSmfName',search:true},
	   				{name:'opStatus',label:'状态',index:'opStatus', width:100,xmlmap:'opStatus',hidden:true,stype:'select',editoptions:{value:operatorStatusValue}},
	   				{name:'opStatusName',label:'状态',index:'opStatusName', width:100,xmlmap:'opStatusName',search:false,align:'center'},
	   				{name:'opTel',label:'电话',index:'opTel', width:100,xmlmap:'opTel'},
	   				{name:'opQQ',label:'QQ',index:'opQQ', width:150, sortable:false,xmlmap:'opQQ'},
	   				{name:'op_roleName',label:'角色',index:'op_roleName', width:150, sortable:false,xmlmap:'op_roleName',search:false}		
	   	],
	   	xmlReader:{
	   		repeatitems:false
	   	},
	   	mtype:'POST',
	   	toolbar:[true,"top"],
	   	autowidth: true,
	   	rowList:[10,20,30],
	   	pager: jQuery('#operatorPager'),
	    viewrecords: true,
	    sortorder: "desc",  
    	caption:"操作用户列表",
    	gridComplete:function(){
    		resizeJQGrid({jqGrid:this.id});
    	}
    	 //用add、modify等布尔值的属性直接控制新增，编辑。如果采用了‘修改密码’那种方式则不能直接用变量控制
    	 //navGrid : function (elem, o, pEdit,pAdd,pDel,pSearch, pView)
	}).navGrid('#operatorPager',
		{add:p_add,edit:p_update,del:false,refresh:p_refurbish,search:p_lookup,addfunc:processAdd,editfunc:processEdit,addtext:'新增',edittext:'编辑',searchtext:'查找',refreshtext:'刷新'},
		null,
		null,
		null,
		{multipleSearch:true,onInitializeSearch:initSearchDialog},//pSearch
		null);
	
	function initSearchDialog(dialog)
	{
		dialog.css(
				{
				 "left":($("body").width() - dialog.outerWidth(true)||0)/2,
				 "top":($("body").height() - dialog.outerHeight(true)||0)/2
				 } 
				);
	}
	
	if(p_delete){
		jQuery("#operatorList").navButtonAdd("#operatorPager",{
			caption : "删除",
			title: '删除所选择记录',
			buttonicon : 'ui-icon-trash',
			onClickButton:processDelete,
			position : "first",
			cursor : 'pointer'
		});
	}
	 
	if(p_updatePassword){
		toolbarButtonAdd("#t_operatorList",{
			caption : "密码",
			title: '修改所选择记录密码',
			buttonicon : 'ui-icon-key',
			onClickButton:processUpdatePassword,
			position : "first",
			cursor : 'pointer'
		});
	}
	
	if(p_lookList){
//		jQuery("#operatorList").navButtonAdd("#operatorPager",{
//			caption : "查看",
//			title: '查看所选择记录明细',
//			buttonicon : 'ui-icon-comment',
//			onClickButton:processDetail,
//			position : "first",
//			cursor : 'pointer'
//		});		
	}
	//roleManage
	if(p_roleManage){
		toolbarButtonAdd("#t_operatorList",{
			caption : "角色",
			title: '角色管理',
			buttonicon : 'ui-icon-role',
			onClickButton:processRole,
			position : "first",
			cursor : 'pointer'
		});
	}
	
	if(p_functionMethodRole){
		toolbarButtonAdd("#t_operatorList",{
			caption : "方法",
			title: '功能方法',
			buttonicon : 'ui-icon-role',
			onClickButton:functionMethod,
			position : "first",
			cursor : 'pointer'
		});
	}
	
	if(p_dataScope){
		toolbarButtonAdd("#t_operatorList",{
			caption : "数据",
			title: '数据范围',
			buttonicon : 'ui-icon-role',
			onClickButton:functionData,
			position : "first",
			cursor : 'pointer'
		});
	}

}
//数据范围弹出窗口
function functionData(){
	//选择一行作为焦点并获取id号
	var selectedRowID = $("#operatorList").getGridParam("selrow");
	 if(!selectedRowID){
		 	//警告对话框的初始化
	 		var alertIDs = {themodal:'alertmod',modalhead:'alerthd',modalcontent:'alertcnt'};
	 		viewModal("#"+alertIDs.themodal,{gbox:"#gbox_"+"priceItemList",jqm:true}); 
	 		$("#jqg_alrt").focus();
	 		return;
	 }
	
	//加载勾选了那些地区前先删除缓存
	$("#tree5 input").each(function(){
		$(this).removeAttr("checked");
	});	
	
	$("#areaTree").dialog("open");
	
	//为每一个街道范围添加单击事件
	$("#tree5 a").each(function(){
		$(this).click(function(){
			if($("#" + $(this).attr("name")).attr("checked") == false){//attr得到checked的属性值如果没有返回false有返回true
				$("#" + $(this).attr("name")).attr("checked","checked");
			}else{
				$("#" + $(this).attr("name")).removeAttr("checked");
			}
		});
	});
	
	$("#tree5 a[title='N'],input[title='N']").each(function(){
		$(this).click(function(){
			var pId = $(this).attr("name");
			//pId = pId.substring(1);//截取第一位字母a 因为id选择器不能选择0所以在非叶子节点id上面加上了字母a
			//用来判断当前点击的对象如果是checkbox则直接判断是否勾选上了。如果勾选了则级联勾选它的下属范围
			//如果点击的对象是a标签则通过得到该标签的name属性值赋给checkebox在判断该checkbox是否勾选
			//两个相邻的input的name值是等于checkbox的id值的
			if($(this).attr("checked") == true || $("#" + $(this).attr("name")).attr("checked") == true){
				$("#tree5 input").each(function(){
					var id = $(this).attr("name");
					id = id.substring(0,id.length - (id.length - pId.length));
					if(pId == id){
						$(this).attr("checked","checked");
					}
				});
			}else{
				$("#tree5 input").each(function(){
					var id = $(this).attr("name");
					id = id.substring(0,id.length - (id.length - pId.length));
					if(pId == id){
						$(this).removeAttr("checked");
					}
				});
			}
		});
	});
	
	$.post(
			"operator.do?type=selectDataScope",
			{
				opId:selectedRowID
			},function(xml){
				var str = $(xml).find("data").text();
				if(str == "nodata"){
					return;
				}
				var s = str.split(",");
				for(var i=0;i<s.length;i=i+1){
					$("#" + s[i]).attr("checked","checked");
				}
			},"xml"
		);

}

//方法管理窗口弹出
function functionMethod(){
	//选择一行作为焦点并获取id号
	var selectedRowID = $("#operatorList").getGridParam("selrow");
	 if(!selectedRowID){
		 	//警告对话框的初始化
	 		var alertIDs = {themodal:'alertmod',modalhead:'alerthd',modalcontent:'alertcnt'};
	 		viewModal("#"+alertIDs.themodal,{gbox:"#gbox_"+"priceItemList",jqm:true}); 
	 		$("#jqg_alrt").focus();
	 		return;
	 }
	 
	//加载前先清掉勾选的缓存
	$("#uu input").removeAttr("checked");
	
	//弹出修改方法的属性菜单
	$("#methodTree").dialog("open");
	$("#methodTree").css("width",screen.width - 305);
	//点击操作后向服务器请求有哪些是已经勾选过的
	$.post(
		"operator.do?type=methodIsCheck",
		{
			id:selectedRowID
		},
		function(xml){	
			var str = $(xml).find("str").text();
			var functionIdStr = $(xml).find("functionId").text();
			var methodIdStr = $(xml).find("methodId").text();
			if(str != "notData"){
				if(functionIdStr != ""){
					functionIdStr = functionIdStr.substring(0,functionIdStr.length - 1);
					functionIdStr = functionIdStr.split(",");
					for(var i=0;i<functionIdStr.length;i=i+1){
						$("#" + functionIdStr[i]).attr("checked","checked");
					}
				}
				
				if(methodIdStr != ""){
					methodIdStr = methodIdStr.substr(0,methodIdStr.length-1);
					methodIdStr = methodIdStr.split(",");//js中字符串拆分数组
					for(var i=0;i<methodIdStr.length;i=i+1){
						$("#" + methodIdStr[i]).attr("checked","checked");
					}
				}
			}
			
			//给功能checkbox增加单击事件
			$(".functionLook").each(function(){				
				$(this).click(function(){
					if(!$(this).attr("checked")){
						$(this).nextAll().removeAttr("checked");
					}
				});
			});
			
			$(".methodLook").each(function(){
				$(this).click(function(){
					if($(this).attr("checked")){
						$("#" + $(this).attr("name")).attr("checked","checked");
					}
				});
			});
		},"xml"
	);
	 
}

//方法勾选保存
function addMethod(){
	/**
	 * 方法的id传递
	 */
	var str = "";
	var temp = "";
	$("#uu input:checked").each(function(){
		if($(this).attr("disabled") == false){
			temp = $(this).val();//把当前勾选的id值赋值个变量temp
			str = str + temp + ",";	
		}
	});

	/**
	 * 功能id传递
	 */
	var str1 = "";
	var temp1 = "";
	$(".functionLook:checked").each(function(){
		temp1 = $(this).val().substring(1);//把当前勾选的id值赋值个变量temp
		str1 = str1 + temp1 + ",";
		while(temp1.length > 2){
			str1 = str1 + temp1.substr(0,temp1.length-2) + ",";
			temp1 = temp1.substr(0,temp1.length-2);
		}
		
	});
	var selectRow = $("#operatorList").getGridParam("selrow");
	$.post(
		"operator.do?type=addMethod",
		{	
			functionStr:str1,
			id:selectRow,
			str:str
		},function(xml){
			 var flag = $(xml).find("flag").text();
			 var body = $(xml).find("body").text();
			 if(flag == 1){		 	
			 	alert("修改成功");
			 	$("#methodTree").dialog("close");
			 }else{
			 	alert(body);
			 }
		},
		"xml"
	);
}

